/*
 * Copyright (C) 2011 Dirk Weißenborn, dirk.weissenborn@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.weissenborn.colabtex.client;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.Cookies;
import com.weissenborn.colabtex.client.events.GroupCreatedEvent;
import com.weissenborn.colabtex.client.events.GroupCreatedEventHandler;
import com.weissenborn.colabtex.client.events.GroupRemovedEvent;
import com.weissenborn.colabtex.client.events.GroupRemovedEventHandler;
import com.weissenborn.colabtex.client.events.LogInEvent;
import com.weissenborn.colabtex.client.events.LogOutEvent;
import com.weissenborn.colabtex.client.events.LoginEventHandler;
import com.weissenborn.colabtex.client.events.LogoutEventHandler;
import com.weissenborn.colabtex.client.view.DashboardView;
import com.weissenborn.colabtex.client.view.DashboardViewImpl;
import com.weissenborn.colabtex.client.view.GroupCreationView;
import com.weissenborn.colabtex.client.view.GroupCreationViewImpl;
import com.weissenborn.colabtex.client.view.GroupListView;
import com.weissenborn.colabtex.client.view.GroupListViewImpl;
import com.weissenborn.colabtex.client.view.GroupManagerView;
import com.weissenborn.colabtex.client.view.GroupManagerViewImpl;
import com.weissenborn.colabtex.client.view.HeaderView;
import com.weissenborn.colabtex.client.view.HeaderViewImpl;
import com.weissenborn.colabtex.client.view.LoginView;
import com.weissenborn.colabtex.client.view.LoginViewImpl;
import com.weissenborn.colabtex.client.view.MainView;
import com.weissenborn.colabtex.client.view.MainViewImpl;
import com.weissenborn.colabtex.client.view.TexView;
import com.weissenborn.colabtex.client.view.TexViewImpl;

/**
 * The Class that implements the ClientFactory
 */
public class ClientFactoryImpl implements ClientFactory {

	/** The event bus. */
	private final EventBus eventBus = new SimpleEventBus();
    
    /** The place controller. */
    private final PlaceController placeController = new PlaceController(eventBus);
	
	/** The group list view. */
	private final GroupListView groupListView = new GroupListViewImpl();
	
	/** The login view. */
	private final LoginView loginView = new LoginViewImpl();
	
	/** The dashboard view. */
	private final DashboardView dashboardView = new DashboardViewImpl();
	
	/** The header view. */
	private final HeaderView headerView = new HeaderViewImpl();
	
	/** The main view. */
	private final MainView mainView = new MainViewImpl();
	
	/** The group creation view. */
	private final GroupCreationView groupCreationView = new GroupCreationViewImpl();
	
	/** The group manager view. */
	private final GroupManagerView groupManagerView = new GroupManagerViewImpl();
	
	/** The tex view. */
	private final TexView texView = new TexViewImpl();
	
	/** The user. */
	private User user = null;
	
	/** The logged in. */
	private boolean loggedIn = false;
	
	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.ClientFactory#addHandlers()
	 */
	public void addHandlers() {
		eventBus.addHandler(LogInEvent.TYPE, new LoginEventHandler() {			
			public void onLoggedIn(LogInEvent event) {
				setUser(event.getUserId());
				loggedIn = true;
			}
		});
		eventBus.addHandler(LogOutEvent.TYPE, new LogoutEventHandler() {
			public void onLoggedOut(LogOutEvent event) {
				user = null;
				loggedIn = false;
				setSessionID(null);
			}
		});
		
		eventBus.addHandler(GroupCreatedEvent.TYPE, new GroupCreatedEventHandler() {	
			public void onGroupCreated(final GroupCreatedEvent event) {
				user.setPermissions();			
			}
		});
		eventBus.addHandler(GroupRemovedEvent.TYPE, new GroupRemovedEventHandler() {
			public void onGroupRemoved(GroupRemovedEvent event) {
				user.setPermissions();			
			}
		});
	}
	
	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.ClientFactory#getEventBus()
	 */
	public EventBus getEventBus() {
		return eventBus;
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.ClientFactory#getPlaceController()
	 */
	public PlaceController getPlaceController() {
		return placeController;
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.ClientFactory#getGroupListView()
	 */
	public GroupListView getGroupListView() {
		return groupListView;
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.ClientFactory#getLoginView()
	 */
	public LoginView getLoginView() {
		return loginView;
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.ClientFactory#getUser()
	 */
	public User getUser() {
		return user;
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.ClientFactory#setUser(java.lang.String)
	 */
	public void setUser(String username) {
		if(username == null) {
			this.user = null;
			return;
		}
		this.user = new User(eventBus);
		this.user.setId(username);		
		this.user.setPermissions();
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.ClientFactory#isLoggedIn()
	 */
	public boolean isLoggedIn() {
		return loggedIn;
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.ClientFactory#setLoggedIn(boolean)
	 */
	public void setLoggedIn(boolean loggedIn) {
		this.loggedIn = loggedIn;
	}
	
	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.ClientFactory#setSessionID(java.lang.String)
	 */
	public void setSessionID(String sessionID) {
		if(sessionID == null)
			Cookies.removeCookie("ethertexSessionID");
		else
			Cookies.setCookie("ethertexSessionID", sessionID);
	}
	
	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.ClientFactory#getSessionID()
	 */
	public String getSessionID() {
		return Cookies.getCookie("ethertexSessionID");
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.ClientFactory#getDashboardView()
	 */
	public DashboardView getDashboardView() {
		return dashboardView;
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.ClientFactory#getHeaderView()
	 */
	public HeaderView getHeaderView() {
		return headerView;
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.ClientFactory#getMainView()
	 */
	public MainView getMainView() {
		return mainView;
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.ClientFactory#getGroupCreationView()
	 */
	public GroupCreationView getGroupCreationView() {
		return groupCreationView;
	}
	
	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.ClientFactory#getGroupManagerView()
	 */
	public GroupManagerView getGroupManagerView() {
		return groupManagerView;
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.ClientFactory#getTexView()
	 */
	public TexView getTexView() {
		return texView;
	}

}
